package com.aia.prm.web.ria.app.ui.file;

import java.util.List;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.domain.VOFile;
import com.aia.prm.persistence.dao.VOFileDAO;
import com.aia.prm.web.ria.app.PrmApplication;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

@SuppressWarnings("serial")
public class FileView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(FileView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	FileList FileList = null;
	Form form = null;
	FileContainer FileContainer = null;
	FileForm FileForm = null;
	private VOFileDAO FileDAO = null;
	List<VOFile> list = null;

	VerticalLayout topLayout = null;
	Button newButton = null;

	Button loadButton = null;

	public FileView(PrmApplication aapp) {
		app = aapp;
		setFileDAO((VOFileDAO) BeanFactory.get().getBean("vOFileDAO"));
		buildMainLayout();
		load();
	}

	void load() {
		list = getFileDAO().selectByExampleWithoutBLOBs(null);
		if (FileContainer == null) {
			try {
				FileContainer = new FileContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		FileContainer.removeAllItems();
		FileContainer.addAll(list);

		FileList.setContainerDataSource(FileContainer);

		FileList.setVisibleColumns(FileContainer.NATURAL_COL_ORDER);
		FileList.setColumnHeaders(FileContainer.COL_HEADERS_SHOW);

		FileForm.setItemDataSource(null);

	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		newButton = new Button("添加记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				FileForm.addObject();
			}
		});
		newButton.setIcon(new ThemeResource("icons/add.png"));
		loadButton = new Button("刷新", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				load();
				int size = list != null ? list.size() : 0;
				app.getMainWindow().showNotification("查找到记录条数：" + size, Notification.TYPE_HUMANIZED_MESSAGE);
			}
		});

		loadButton.setIcon(new ThemeResource("icons/clock.png"));
		// topLayout.addComponent(newButton);
		// topLayout.addComponent(plansBox);
		HorizontalLayout topLayoutSub = new HorizontalLayout();
		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(true);
		topLayoutSub.setSpacing(true);

		topLayoutSub.addComponent(newButton);
		topLayoutSub.addComponent(loadButton);

		topLayout.addComponent(topLayoutSub);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		FileList = new FileList(app, this);
		horiz.setFirstComponent(FileList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		FileForm = new FileForm(this);

		rightLayout.addComponent(FileForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = FileList.getItem(FileList.getValue());
		FileForm.setItemDataSource(item);
	}

	public VOFileDAO getFileDAO() {
		return FileDAO;
	}

	public void setFileDAO(VOFileDAO FileDAO) {
		this.FileDAO = FileDAO;
	}

}
